package cn.lg.soar.system.account.controller;

import cn.lg.soar.common.exceptions.BaseAException;
import cn.lg.soar.common.model.HttpResult;
import cn.lg.soar.core.annotations.ResourceScanIgnore;
import cn.lg.soar.system.account.model.AccountLoginDTO;
import cn.lg.soar.system.account.service.ICodeLoginService;
import cn.lg.soar.system.account.service.ILoginService;
import cn.lg.soar.system.api.constant.ModuleConst;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * @author luguoxiang
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ResourceScanIgnore
@Api(tags = {"授权接口"})
@RestController
@RequestMapping(ModuleConst.MODULE_NAME + "/login")
public class LoginController {

    @Autowired
    private ILoginService loginService;
    @Autowired
    private ICodeLoginService codeLoginService;

    @ApiOperation("账号密码登录")
    @PostMapping(value = "account", name = "账号密码登录")
    public HttpResult<String> account(@RequestBody @Valid AccountLoginDTO dto) {
        return HttpResult.data(this.loginService.login(dto));
    }


    @ApiOperation("refreshToken登录")
    @GetMapping(value = "refreshToken", name = "refreshToken登录")
    public HttpResult<String> refreshToken(@RequestParam String refreshToken) {
        return HttpResult.data(this.loginService.refresh(refreshToken));
    }

    @ApiOperation("授权码登录")
    @GetMapping(value = "code", name = "授权码登录")
    public HttpResult<String> code(@RequestParam String code, HttpServletRequest request) {
        String token = codeLoginService.login(request, code);
        if (token == null) {
            return HttpResult.fail(new BaseAException(411, "未扫码授权"));
        }
        return HttpResult.data(token);
    }

}
